package com.hq.fxh.CustomerView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hq.fxh.HttpApi.ArticleServiceImpl;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.AutoNewsModle;
import com.hq.fxh.Modle.SelectByCategoryIdRes;
import com.hq.fxh.Modle.ShareArticleRes;
import com.hq.fxh.No0_ChengQu.Adapter.Adapter_ShareMenu;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.AdEditorActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.CommonRes;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.GetAdvertByUidRes;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.ShareInfo;
import com.hq.fxh.R;
import com.hq.fxh.Utils.ShareUtils;
import com.hq.fxh.Utils.IntentUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;


/**
 * @author wanglubin
 * @ClassName ShareDialogUtils
 * @Description :分享弹窗工具类
 * @Created 2015/11/18
 * @Email 422695989@qq.com
 */
public class ShareDialogUtils {

    ProgressDialog progressDialog;
    Context context;
    int uid;
    String newUrl;
    private ShareInfo shareInfo;
    View v;

    public ShareDialogUtils(Context context, View view) {

        this.context = context;

        shareInfo = new ShareInfo();
        v = view;
    }

    public void shareAd( ProgressDialog progressDialog, int uid, String newUrl) {
        this.uid = uid;
        this.newUrl = newUrl;
        this.progressDialog = progressDialog;
        setAddADShare();
    }

    public void share(AutoNewsModle shareData) {
        setShareDate(shareData);
        showShareView(context, v, sharePopCallBack);

    }

    public void setAddADShare() {

        UserServiceImpl.getAdTemp(uid + "", new NetworkSubscriber<GetAdvertByUidRes>() {
            @Override
            public int onSuccess(GetAdvertByUidRes data) {
                super.onSuccess(data);
                LogUtils.e("setaddADShareSuccess");
                if (data != null && data.getCode() == 0) {
                    if ((data.getData().getBottom().size() > 0) || (data.getData().getTop().size() > 0)) {
                        pasterNewsUrl(newUrl);
                    } else {
                        IntentUtils.openActivity(context, AdEditorActivity.class);
                    }
                } else {
                    ToastUtils.show(context, data.getMsg());
                    progressDialog.dismiss();

                }
                return 0;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ToastUtils.show(context, "获取广告模板失败");
                progressDialog.dismiss();
            }
        });

    }


    public void pasterNewsUrl(String url) {
        /** 用户粘贴文章文章操作 */
        ArticleServiceImpl.pasterNewsUrl(url, new NetworkSubscriber<CommonRes>() {
            @Override
            public int onSuccess(CommonRes data) {
                super.onSuccess(data);
                LogUtils.e("pasterNewsUrl__onSuccess");

                if (data != null && data.getCode() == 0) {
                    String key = data.getData().toString();
                    shareArticle(key);
                } else {
                    progressDialog.dismiss();

                    ToastUtils.show(context, data.getMsg());
                }
                return 0;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtils.e("pasterNewsUrl__onError");

                progressDialog.dismiss();

                ToastUtils.show(context, "植入广告失败");

            }
        });
    }

    public void shareArticle(String key) {
        ArticleServiceImpl.shareArticle(uid + "", key, 0, new NetworkSubscriber<ShareArticleRes>() {
            @Override
            public int onSuccess(ShareArticleRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    ShareArticleRes.DataBean shareData = data.getData();
                    progressDialog.dismiss();
                    setShareDate(shareData);
                    showShareView(context, v, sharePopCallBack);

                } else {
                    progressDialog.dismiss();
                    ToastUtils.show(context, data.getMsg());
                }
                return 0;
            }
        });
    }


    private Bitmap bitmap;
    ShareDialogUtils.SharePopCallBack sharePopCallBack = new ShareDialogUtils.SharePopCallBack() {
        @Override
        public void Call(AdapterView<?> adapterView, View view, int i, long l) {

            switch (i) {
                case 0:
                    ShareUtils.share((Activity) context, shareInfo);

                    break;
                case 1:
                    ShareUtils.toolShareWechat((Activity) context, shareInfo);

                    break;
                case 2:
                    ShareUtils.shareQQ(shareInfo);

                    break;
                case 3:
                    ShareUtils.shareQZone(shareInfo);

                    break;
                case 4:

                    if (shareInfo.getImgUrl() != null) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                bitmap = Tool.getbitmapFromUrl(shareInfo.getImgUrl());


                                ShareUtils.shareSinaWeibo(context, shareInfo, bitmap);
                                handler.sendEmptyMessage(0);
                            }
                        }).start();

                    }
                    break;
                case 5:
                    ShareUtils.shareSinaWeiboMessage(shareInfo);

                    break;


            }

        }
    };
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ShareUtils.shareSinaWeibo(context, shareInfo, bitmap);

        }
    };


    public void setShareDate(SelectByCategoryIdRes.ValueBean shareData) {

        if (shareData != null) {
            shareInfo = new ShareInfo();
            shareInfo.setUrl(shareData.getShare_url());
            //   shareInfo.setArticleId(Tool.getIntFromString(shareData.getAdvert_id()));


            if (shareData.getImage() != null && shareData.getImage().size() != 0) {
                shareInfo.setImgUrl(shareData.getImage().get(0));
            }


            if (TextUtils.isEmpty(shareData.getDes().trim())) {

                shareInfo.setText(shareData.getTitle());
            } else {

                String des = shareData.getDes();

                shareInfo.setText(des.substring(0, 20));
            }
            shareInfo.setTitle(shareData.getTitle());
        }

    }

    public void setShareDate(ShareArticleRes.DataBean shareData) {

        if (shareData != null) {
            shareInfo = new ShareInfo();
            shareInfo.setUrl(shareData.getShare_url());
            shareInfo.setArticleId(shareData.getArticle_member_id());
            shareInfo.setImgUrl(shareData.getTitle_pic_url());
            if (TextUtils.isEmpty(shareData.getSummary().trim())) {
                shareInfo.setText(shareData.getTitle());
            } else {
                shareInfo.setText(shareData.getSummary());
            }

            shareInfo.setTitle(shareData.getTitle());
        }

    }


    public static String[] titles;

    static {
        titles = new String[]{"微信好友", "微信朋友圈", "QQ", "QQ空间", "新浪微博", "复制链接"};
    }

    public static PopupWindow popupWindow;

    /**
     * 显示分享
     *
     * @param context
     */

    public static PopupWindow showShareView(final Context context, final View v, final SharePopCallBack sharePopCallBack) {
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.view_share, null);
        XGridView gvShare = (XGridView) view.findViewById(R.id.gvShare);
        Button btnCancel = (Button) view.findViewById(R.id.btnCancel);


        int[] resIds = new int[]{R.mipmap.iconfont_weixin, R.mipmap.iconfont_wei_xin_cicle, R.mipmap.iconfont_qq, R.mipmap.iconfont_qqkongjian, R.mipmap.iconfont_weibo, R.mipmap.iconfont_lianjie};

        Adapter_ShareMenu adapter = new Adapter_ShareMenu(resIds, titles, (Activity) context);
        gvShare.setAdapter(adapter);
        gvShare.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                dismiss();
                sharePopCallBack.Call(adapterView, view, i, l);
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setContentView(view);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha((Activity) context, 1f);
            }
        });
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);

        popupWindow.showAtLocation(v,
                Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, 0);
        Tool.setAlpha((Activity) context, 0.6f);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        return popupWindow;
    }

    public static void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    public interface SharePopCallBack {
        void Call(AdapterView<?> adapterView, View view, int position, long l);
    }


}
